module.exports = function (app, models, $, db, nm) {
    
    // 初始化数据库时处理重复的email
    app.post('/init', async function (req, res) {
        try {
            // 获取所有学生数据
            let students = await models.sp_students.listSync({});
            let emailSet = new Set();
            let duplicates = [];

            students.rows.forEach(student => {
                if (emailSet.has(student.email)) {
                    duplicates.push(student);
                } else {
                    emailSet.add(student.email);
                }
            });

            // 处理重复的email
            for (let duplicate of duplicates) {
                // 可以选择删除重复项或修改email
                // 这里选择删除重复项
                await models.sp_students.removeSync({ id: duplicate.id });
            }

            res.send({ message: '初始化完成' });
        } catch (err) {
            res.send({ errorMsg: err.message });
        }
    });

}